var tabIds = [];
var tabs = {};
var currentPlayingTabIndex = -1;
var currentPlayingVideoIndex = -1;
var currentPlayingEmbeddedIndex = -1;

function pauseVideo(request) {
  if (request.videoid && request.videoid != undefined) {
	currentPlayingVideoIndex = request.videoid;
  }
  if (request.embedid && request.embedid != undefined) {
	currentPlayingEmbeddedIndex = request.embedid;
  }
  console.log("Currently playing video : " + currentPlayingVideoIndex + " : " + currentPlayingEmbeddedIndex  );
  createTabList();
  for(var i = 0; i < tabIds.length; i++){
	chrome.tabs.sendRequest(tabIds[i], {action: "pauseVideo"});
  }
}

function createTabList() {
  chrome.windows.getAll({ populate: true }, function(windowList) {
    tabs = {};
    tabIds = [];
    for (var i = 0; i < windowList.length; i++) {
      for (var j = 0; j < windowList[i].tabs.length; j++) {
        tabIds[tabIds.length] = windowList[i].tabs[j].id;
        tabs[windowList[i].tabs[j].id] = windowList[i].tabs[j];
      }
    }
  });
}

chrome.browserAction.onClicked.addListener(pauseVideo);
chrome.extension.onRequest.addListener(pauseVideo);

//$%@#$@#$@#$@#$@#$
//on currentfocus change, tab id changes?!
